﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Moon.Common;
using Model;
using System.Collections.Generic;
using Moon.Common.WebInfo;
using System.IO;
using Server;
using GPI.Server;

public partial class Admin_SystemManage_NodeManage : System.Web.UI.UserControl
{
    //类帮助服务
    GPI.Server.ClassService ClassServer = new GPI.Server.ClassService();
    //节点帮助服务
    GPI.Server.NodeService NodeServer = new GPI.Server.NodeService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindList();
            BindNode();
        }

    }
    #region 按钮事件
    /// <summary>
    /// 添加类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnAddClass_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
    }
    /// <summary>
    /// 保存類別
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveClass_Click(object sender, EventArgs e)
    {
        Model.ClassInfo model = new Model.ClassInfo();
        model.ID = this.btnSaveClass.CommandArgument.ToInt(0);
        model.ClassName = this.txtClassName.Text;
        bool isTrue = false;
        if ("0".Equals(this.btnSaveClass.CommandArgument))
        {
            isTrue = ClassServer.Add(model);
        }
        else
        {
            isTrue = ClassServer.Update(model);
        }
        if (isTrue)
            MessageBox.Show(this.Page, "操作成功！");
        else
            MessageBox.Show(this.Page, "操作失敗！");
        BindList();

    }
    /// <summary>
    /// 修改类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnEditClass_Click(object sender, EventArgs e)
    {

        TreeNode node = this.tvClass.SelectedNode;
        if (node != null && node.Parent == null)
        {
            Model.ClassInfo model = new Model.ClassInfo();
            model = ClassServer.GetModel(node.Value.ToInt(0));
            this.btnSaveClass.CommandArgument = model.ID.ToString();
            this.txtClassName.Text = model.ClassName;
            this.MultiView1.ActiveViewIndex = 1;
        }
        else
        {
            MessageBox.Show(this.Page, "請選擇根節點，當前不選擇不存在或不為類別！");
        }
    }

    /// <summary>
    /// 选择节点时绑定频道数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void tvClass_SelectedNodeChanged(object sender, EventArgs e)
    {
        TreeView model = (TreeView)sender;
        ///绑定根节点ID
        if (model.SelectedNode.Parent == null)
        {
            this.hfClassId.Value = model.SelectedNode.Value;
            this.repNodeList.DataSource = NodeServer.GetAllListByClassID(model.SelectedNode.Value).Tables[0];
            this.repNodeList.DataBind();
        }
        else
        {
            this.repNodeList.DataSource = NodeServer.GetAllListByCh(model.SelectedNode.Value).Tables[0];
            this.repNodeList.DataBind();
        }


    }
    /// <summary>
    /// 进入添加频道界面
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEnterAddNode_Click(object sender, EventArgs e)
    {
        Clear();
        this.btnSaveNode.CommandArgument = "0";
        this.ddlModelType.DataSource = SystemConfigInfo.TypeInfoMap;
        this.ddlModelType.DataTextField = "Text";
        this.ddlModelType.DataValueField = "Value";
        this.ddlParentID.DataSource = NodeServer.GetAllNodeByHeard().Tables[0];
        this.ddlParentID.DataValueField = "Id";
        this.ddlParentID.DataTextField = "NodeName";

        this.ddlTemplate.DataSource = new TemplateService().GetList("TempletaType=1");
        this.ddlTemplate.DataTextField = "TempletaName";
        this.ddlTemplate.DataValueField = "ID";
        this.ddlTemplate.DataBind();
        this.ddlParentID.DataBind();
        this.ddlModelType.DataBind();

        this.ddlParentID.Items.Add(new ListItem("主頻道", "0"));
        //如果选定的频道不为空的时候
        if (this.tvClass.SelectedNode != null && this.ddlParentID.Items.FindByValue(this.tvClass.SelectedNode.Value) != null)
        {
            this.ddlParentID.SelectedValue = this.tvClass.SelectedNode.Value;
 
        }


        
        this.MultiView1.ActiveViewIndex = 2;
    }

    /// <summary>
    /// 保存频道或栏目
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveNode_Click(object sender, EventArgs e)
    {
        NodeInfo model = new NodeInfo();
        model.Id = this.btnSaveNode.CommandArgument.ToInt(0);
        if (model.Id != 0)
        {
            model = NodeServer.GetModel(model.Id);
        }
        model.IsHTML = false;
        model.HeardSequence = this.txtHeardSequence.Text;
        model.IsShow = this.cbIsShow.Checked;
        model.MateKey = this.txtMATEKEY.Text.HtmlEncode();
        model.MateMark = this.txtMATEMark.Text.HtmlEncode();
        model.NodeName = this.txtNodeName.Text;
        model.OrderBy = NodeServer.GetRecordCount("") + 1;
        model.ParentID = this.ddlParentID.SelectedValue.ObjectToInt(0);
        model.ShowTitle = this.txtShowTitle.Text;
        model.Template = this.ddlTemplate.SelectedValue.ToInt(0);
        model.UpdateDate = new DateTime();
        model.ClassID = this.hfClassId.Value.ToInt(0);
        model.Content = this.txtContent.Text;
        model.ModelType = this.ddlModelType.SelectedValue;
        model.IsHeard = this.cbIsHeard.Checked;
        bool isAdd = false;
        if ("0".Equals(model.Id.ToString()))
        {
            isAdd = NodeServer.Add(model);
        }
        else
        {
            if (!model.IsHTML)
            {
                model.Url = WebConfigInfo.ShowAction + model.Id;
            }
            else
            { }
            isAdd = NodeServer.Update(model);
        }
        if (isAdd)
            MessageBox.Show(this.Page, "操作成功！");
        else
            MessageBox.Show(this.Page, "操作失敗！");

        BindList();
        BindNode();

    }
    /// <summary>
    /// 频道列表操作
    /// </summary>
    /// <param name="source"></param>
    /// <param name="e"></param>
    protected void repNodeList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Model.NodeInfo model = NodeServer.GetModel(e.CommandArgument.ObjectToInt(0));
        switch (e.CommandName)
        {
            case "edit":

                this.MultiView1.ActiveViewIndex = 2;


                Clear();
                this.btnSaveNode.CommandArgument = "0";
                this.ddlModelType.DataSource = SystemConfigInfo.TypeInfoMap;
                this.ddlModelType.DataTextField = "Text";
                this.ddlModelType.DataValueField = "Value";
                this.ddlParentID.DataSource = NodeServer.GetAllNodeByHeard().Tables[0];
                this.ddlParentID.DataValueField = "Id";
                this.ddlParentID.DataTextField = "NodeName";

                this.ddlTemplate.DataSource = new TemplateService().GetList("TempletaType=1");
                this.ddlTemplate.DataTextField = "TempletaName";
                this.ddlTemplate.DataValueField = "ID";
                this.ddlTemplate.DataBind();
                this.ddlParentID.DataBind();
                this.ddlModelType.DataBind();
                this.ddlParentID.Items.Add(new ListItem("主頻道", "0"));
                //如果存在选定，把自己砍掉。

                this.ddlParentID.Items.Remove(this.ddlParentID.Items.FindByValue(model.Id.ToString()));

                this.btnSaveNode.CommandArgument = model.Id.ToString();
                this.txtHeardSequence.Text = model.HeardSequence;

                this.cbIsShow.Checked = model.IsShow;
                this.txtMATEKEY.Text = model.MateKey.HtmlDecode();
                this.txtMATEMark.Text = model.MateMark.HtmlDecode();
                this.txtNodeName.Text = model.NodeName;
                this.cbIsHeard.Checked = model.IsHeard;
                this.txtShowTitle.Text = model.ShowTitle;

             

                this.ddlParentID.SelectedValue = model.ParentID != 0 ? model.ParentID.ToString() : "0";
                this.ddlTemplate.SelectedValue = model.Template.Value.ToString();
                if (this.ddlModelType.Items.FindByValue(model.ModelType)!=null)
                    this.ddlModelType.SelectedValue = model.ModelType;

                this.hfClassId.Value = model.ClassID.ToString();
                this.txtContent.Text = model.Content.HtmlDecode();



                break;
            case "delete":
                if (NodeServer.Delete(e.CommandArgument.ObjectToInt(0)))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                this.repNodeList.DataSource = NodeServer.GetAllListByCh(model.Id.ToString());
                BindNode();
                BindList();
                break;
            case "Moveup":
                model.OrderBy = model.OrderBy + 1;

                if (NodeServer.Update(model))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                BindList();
                tvClass.ExpandAll();
                break;
            case "Movedown":
                model.OrderBy = model.OrderBy-2;

                if (NodeServer.Update(model))
                {
                    MessageBox.Show(this.Page, "操作成功！");
                }
                else
                {
                    MessageBox.Show(this.Page, "操作失敗！");
                }
                BindList();
                tvClass.ExpandAll();
                break;
        }

    }
    /// <summary>
    /// 删除类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnDeleteClass_Click(object sender, EventArgs e)
    {
        if ( ClassServer.Delete(ClassServer.GetModel(this.tvClass.SelectedNode.Value.ToInt(0), this.tvClass.SelectedNode.Text).ID))
        {
            MessageBox.Show(this.Page, "操作成功！");
        }
        else
        {
            MessageBox.Show(this.Page, "操作失敗！");
        }
        BindList();
            
    }
    #endregion
    /// <summary>
    /// 绑定列表
    /// </summary>
    private void BindList()
    {
        /*绑定列表*/
        //清空节点
        this.tvClass.Nodes.Clear();
        int i = 0;
        //获得列表
        List<ClassInfo> list = ClassServer.GetModelList("");
        foreach (ClassInfo tmp in list)
        {
            TreeNode node = new TreeNode();
            if (i == 0) this.hfClassId.Value = tmp.ID.ToString();
            node.Selected = i == 0 ? true : false;
            i++;
            node.Value = tmp.ID.ToString();
            node.Text = tmp.ClassName;
            foreach (TreeNode chNode in NodeServer.GetAllNode(tmp.ID))
            {
                node.ChildNodes.Add(chNode);
            }
            this.tvClass.Nodes.Add(node);
        }

        this.MultiView1.ActiveViewIndex = 0;

    }
    /// <summary>
    /// 绑定节点
    /// </summary>
    private void BindNode()
    {
        if (this.tvClass.SelectedNode != null)
        {
            this.repNodeList.DataSource = NodeServer.GetAllListByClassID(this.tvClass.SelectedNode.Value.ToString()).Tables[0];
            this.repNodeList.DataBind();
        }
        tvClass.ExpandAll();
        this.MultiView1.ActiveViewIndex = 0;
    }
    /// <summary>
    /// 清理操作
    /// </summary>
    private void Clear()
    {
        this.txtClassName.Dispose();
    }


    /// <summary>
    /// 返回列表
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbnReturnList_Click(object sender, EventArgs e)
    {
        BindNode();
    }
}


